﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using XeBus.BusinessObject;

namespace XeBus.DataAccess
{
    public class ConDuongDAO
    {
        private ConnectData _conn;
        public ConDuongDAO()
        {
            _conn = new ConnectData();
        }

        // Hàm thêm mới một con đường
        public void THEM_CON_DUONG(ConDuong cd)
        {

            SqlParameter[] param = new SqlParameter[2];

            param[0] = new SqlParameter("P_TEN_DUONG", SqlDbType.NVarChar);
            param[0].Value = cd.Ten_Duong;

            param[1] = new SqlParameter("P_QUAN_HUYEN", SqlDbType.NVarChar);
            param[1].Value = cd.Quan_Huyen;

            _conn.ExecuteNonQuery("THEM_CONDUONG", param);

        }

        public DataTable ConDuong()
        {
            return _conn.ExecuteSelectQuery("SELECT * FROM ConDuong", null);
        }

        public DataTable ConDuongTheoID(string id)
        {
            return _conn.ExecuteSelectQuery("SELECT * FROM ConDuong WHERE Ma_Con_Duong=" + id + "", null);
        }

        public void SUA_LO_TRINH(ConDuong cd)
        {
            SqlParameter[] param = new SqlParameter[3];

            param[0] = new SqlParameter("P_TEN_DUONG", SqlDbType.NVarChar);
            param[0].Value = cd.Ten_Duong;

            param[1] = new SqlParameter("P_QUAN_HUYEN", SqlDbType.NVarChar);
            param[1].Value = cd.Quan_Huyen;

            param[2] = new SqlParameter("P_MA_CON_DUONG", SqlDbType.Int);
            if (cd.Ma_Con_Duong.Length > 0)
                param[2].Value = cd.Ma_Con_Duong;
            else
                param[2].Value = DBNull.Value;

            _conn.ExecuteNonQuery("SUA_CONDUONG", param);
        }

        public void XOA(string id)
        {
            _conn.ExecuteNonQueryString("DELETE FROM ConDuong WHERE Ma_Con_Duong=" + id + ";");
        }
    }
}
